package payment

import (
	"encoding/json"
	"github.com/astaxie/beego"
	"log"
	"net/http"
	"nft-server/errcode"
	"time"
)

//第三方支付方式
const (
	WechatPay = "wechatpay" //微信支付
	AliPay    = "alipay"    //支付宝
)

type PaymentMessage struct {
	OrderId     string `json:"orderId"`
	OrderStatus string `json:"orderStatus"`
}

//商户相关信息
type PaymentConfig struct {
	AppID                      string //公众号ID
	MchID                      string // 商户号
	MchCertificateSerialNumber string // 商户证书序列号
	MchAPIv3Key                string // 商户APIv3密钥
	PrivateKeyPath             string //商户私钥路径
}

//预支付请求参数
type PrepayInfo struct {
	Description   string    `json:"description"`
	OutTradeNo    string    `json:"out_trade_no"`
	TimeExpire    time.Time `json:"time_expire"`
	NotifyUrl     string    `json:"notify_url"`
	Price         float64   `json:"total"`
	Currency      string    `json:"currency"`
	PaymentMethod string    `json:"paymentMethod"`
}

//第三方支付通知系统时的报文
type OrderNotifyReq struct {
	Id           string              `json:"id"`
	CreatTime    string              `json:"creat_time"`
	EventType    string              `json:"event_type"`
	ResourceType string              `json:"resource_type"`
	Resource     OrderNotifyResource `json:"resource"`
	Summary      string              `json:"summary"`
}
type OrderNotifyResource struct {
	Algorithm      string `json:"algorithm"`
	Ciphertext     string `json:"ciphertext"`
	AssociatedData string `json:"associated_data"`
	OriginalType   string `json:"original_type"`
	Nonce          string `json:"nonce"`
}

//回复报文
type PaymentResponse struct {
	Code string `json:"code"`
	Msg  string `json:"message"`
}

type PaymentCommonController struct {
	beego.Controller
}

const (
	SuccessCode = 0
	FailedCode  = 1
	SuccessMsg  = "success"
	FailedMsg   = "failed"
	PaymentFail = "FAIL"
)

type Response struct {
	Code int         `json:"code"`
	Msg  string      `json:"msg"`
	Data interface{} `json:"data,omitempty"`
}

//处理第三方支付通知
func (pc *PaymentCommonController) HandlePaymentNotifyError(errcode errcode.Status) {
	pc.Ctx.ResponseWriter.WriteHeader(int(errcode.Code))
	data := &PaymentResponse{PaymentFail, errcode.Message}
	res, err := json.MarshalIndent(data, "", "\t")
	if err != nil {
		log.Fatalf("HandleErrCode MarshalIndent error: %v", err)
	}
	pc.Ctx.ResponseWriter.Write(res)
}

func (pc *PaymentCommonController) HandlePaymentNotifySuccess() {
	pc.Ctx.ResponseWriter.WriteHeader(http.StatusOK)
	data := &PaymentResponse{"", ""}
	res, err := json.MarshalIndent(data, "", "\t")
	if err != nil {
		log.Fatalf("HandleStandObj MarshalIndent error: %v", err)
	}
	pc.Ctx.ResponseWriter.Write(res)
}

// HandleBusinessErr is a function
func (pc *PaymentCommonController) HandleErrCode(errcode errcode.Status) {
	pc.Ctx.ResponseWriter.WriteHeader(int(errcode.Code))
	data := &Response{FailedCode, errcode.Message, nil}
	res, err := json.MarshalIndent(data, "", "\t")
	if err != nil {
		log.Fatalf("HandleErrCode MarshalIndent error: %v", err)
	}
	pc.Ctx.ResponseWriter.Write(res)
}

// HandleBusinessObj is a function
func (pc *PaymentCommonController) HandleBusinessObj(obj interface{}) {
	pc.Ctx.ResponseWriter.WriteHeader(http.StatusOK)
	data := &Response{SuccessCode, SuccessMsg, obj}
	res, err := json.MarshalIndent(data, "", "\t")
	if err != nil {
		log.Fatalf("HandleStandObj MarshalIndent error: %v", err)
	}
	pc.Ctx.ResponseWriter.Write(res)
}

func (pc *PaymentCommonController) HandleBusinessSuccess() {
	pc.Ctx.ResponseWriter.WriteHeader(http.StatusOK)
	data := &Response{SuccessCode, SuccessMsg, nil}
	res, err := json.MarshalIndent(data, "", "\t")
	if err != nil {
		log.Fatalf("HandleStandObj MarshalIndent error: %v", err)
	}
	pc.Ctx.ResponseWriter.Write(res)
}
